package com.aerfazhe.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Author：wk
 * @Slogan：无论风雨，和自己一决胜负吧
 * @Create：2022/6/30/14:13
 * @Description：自定义全局过滤器
 * @Version：1.0
 */
//@Component
@Slf4j
public class CheckAuthFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        校验请求中的token
        List<String> token = exchange.getRequest().getHeaders().get("token");
        log.info("token:"+token);
        if (CollectionUtils.isEmpty(token)) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}
